package framework
{
    import flash.events.Event;

    import mx.core.mx_internal;
    import mx.messaging.messages.IMessage;
    import mx.rpc.AsyncToken;
    import mx.rpc.Fault;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    use namespace mx_internal;

    public dynamic class ArtificialToken extends AsyncToken
    {
        private var resultProperty:String;

        public var _host:Object;

        public function ArtificialToken(resultProperty:String = null, host:Object = null, message:IMessage = null)
        {
            super(message);
            this.resultProperty = resultProperty;
            _host = host;
        }

        public function resultHandler(e:Event):void
        {
            var host:Object = _host;
            if (!host)
                host = e;
            var r:* = Utils.getProperty(host, resultProperty);
            resultHandlerForResult(r);
        }
        
        public function resultHandlerForResult(result:*):void
        {
        	var re:ResultEvent = ResultEvent.createEvent(result, this, message);
            mx_internal::applyResult(re);
        }
        

        public function faultHandler(e:Event):void
        {
            var fault:Fault = new Fault("ArtificialToken.Fault", e.toString());
            var fe:FaultEvent = FaultEvent.createEvent(fault, this, message);
            mx_internal::applyFault(fe);
        }
    }
}